package com.cn.ccp.action.classschedule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.ClassScheduleSettingsBean;

/**
 * 时间表action
 * @author renyongchengO
 *O
 */
public class ClassScheduleSettingAction extends BaseAction{
	private Logger log = Logger.getLogger(ClassScheduleSettingAction.class);
    private ClassScheduleSettingsBean classScheduleSettingsBean;
	private List<ClassScheduleSettingsBean> classScheduleList;
    
    public ClassScheduleSettingAction(){
    	getpagedetail();
    }
    	 
	
	public void getpagedetail() {
		setItemCount(getScheduleSettingService().getCount());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		classScheduleList = getScheduleSettingService().pageQuery(start, pagesize);
		
	}
	
	public String edit(){
		log.info("--------edit()-------"+classScheduleSettingsBean);
		 
		if(classScheduleSettingsBean.getClassScheduleType() == null || classScheduleSettingsBean.getClassScheduleType().equals("")){
			showMsg("termType", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(classScheduleSettingsBean.getStartTime().equals("")){
			showMsg("startTime", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(classScheduleSettingsBean.getEndTime().equals("")){
			showMsg("endTime", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(classScheduleSettingsBean.getId() == 0)
			return save();
		return update();
	}
	public String save(){
		boolean flag = getScheduleSettingService().save(classScheduleSettingsBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag = getScheduleSettingService().update(classScheduleSettingsBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	

	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		log.info("---------------------------------"+id);
		if(id != null && !"".equals(id.trim())){
			classScheduleSettingsBean =  (ClassScheduleSettingsBean)getScheduleSettingService().findClassScheduleSettingsById(Integer.parseInt(id));
		}else{
			classScheduleSettingsBean = new ClassScheduleSettingsBean();
			classScheduleSettingsBean.setNumber(getScheduleSettingService().getCount());
		}
		log.info("-----------------"+classScheduleSettingsBean);
		return UPDATE;
	}
	
	
	
	public void doDelete(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		getScheduleSettingService().delete(Integer.parseInt(id));
		getpagedetail();
		log.info("-----------------doDelete()----------------"+id);
	}


	public ClassScheduleSettingsBean getClassScheduleSettingsBean() {
		if(classScheduleSettingsBean == null)
			classScheduleSettingsBean = new ClassScheduleSettingsBean();
		return classScheduleSettingsBean;
	}


	public void setClassScheduleSettingsBean(
			ClassScheduleSettingsBean classScheduleSettingsBean) {
		this.classScheduleSettingsBean = classScheduleSettingsBean;
	}


	public List<ClassScheduleSettingsBean> getClassScheduleList() {
		if(classScheduleList == null)
			classScheduleList = new ArrayList<ClassScheduleSettingsBean>();
		return classScheduleList;
	}


	public void setClassScheduleList(
			List<ClassScheduleSettingsBean> classScheduleList) {
		this.classScheduleList = classScheduleList;
	}
	
	
}
